home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / HyperCard CTB Toolkit 1.0b2 / Source Code / CTBSendBytes.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  4.4 KB  |  152 lines  |  [TEXT/MPS ]

  1. (*
  2.     CTBSendBytes byteList[,eom] -- Send each byte in the list out the current connection. If the eom
  3.         parameter is present and non-empty, then set the end-of-message flag.
  4.  
  5.     To compile and link this file using Macintosh Programmer's Workshop,
  6.  
  7.         pascal -w CTBSendBytes.p
  8.         link -m ENTRYPOINT -o HyperCommands -rt XCMD=2763 -sn Main=CTBSendBytes ∂
  9.             CTBSendBytes.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  10.  
  11.     © Copyright 1990 by Apple Computer, Inc.
  12.  
  13.     Initial coding 2/90 by Harry R. Chesley.
  14. *)
  15.  
  16. {$R-}
  17.  
  18. {$S CTBSendBytes }     { Segment name must be the same as the command name. }
  19.  
  20. unit DummyUnit;
  21.  
  22. interface
  23.  
  24. uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, HyperXCmd;
  25.  
  26. procedure EntryPoint(paramPtr: XCmdPtr);
  27.     
  28. implementation
  29.  
  30. const
  31.  
  32. space = ord(' ');        { Space. }
  33. comma = ord(',');        { Comma. }
  34. zero = ord('0');        { Zero. }
  35. nine = ord('9');            { Nine. }
  36. lowera = ord('a');        { Lower-case a. }
  37. lowerf = ord('f');        { Lower-case f. }
  38. upperA = ord('A');    { Upper-case A. }
  39. upperF = ord('F');        { Upper-case F. }
  40. dollar = ord('$');        { Dollar sign. }
  41.  
  42. procedure CTBSendBytes(paramPtr: XCmdPtr); forward;
  43.  
  44. procedure EntryPoint(paramPtr: XCmdPtr);
  45.  
  46.     begin
  47.         CTBSendBytes(paramPtr);
  48.     end;
  49.  
  50. procedure CTBSendBytes(paramPtr: XCmdPtr);
  51.  
  52.     {$I CTBUtil.inc}
  53.  
  54.     var i: integer;
  55.         ioBuffer: Ptr;
  56.         ioSize: longInt;
  57.         err: CMErr;
  58.         h: Handle;
  59.         flags: integer;
  60.  
  61.     procedure Fail(errMsg: Str255); { set theResult and quit }
  62.         begin
  63.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  64.             exit(CTBSendBytes);
  65.         end;
  66.  
  67.     function copyAndCount(copyFrom, copyTo: Ptr; doTheCopy: boolean): longInt;
  68.         { Copy and translate bytes from the source string to the destination, counting the size of the destination
  69.             as we go. If doTheCopy is not true, then don't actually do the copy, just accumulate the count. }
  70.  
  71.         var fromPtr, toPtr, nextFromPtr: Ptr;
  72.             base, tot: integer;
  73.             theChar: SignedByte;
  74.  
  75.         begin
  76.             { Cycle thru all the bytes to be copied. }
  77.             fromPtr := copyFrom; toPtr := copyTo;
  78.             while fromPtr^ <> 0 do
  79.                 begin
  80.                     { Skip over leading junk. }
  81.                     while not (fromPtr^ in [0,zero..nine,lowera..lowerf,upperA..upperF,comma,dollar]) do
  82.                         fromPtr := pointer(ord4(fromPtr)+1);
  83.                     { Get the next byte. }
  84.                     base := 10;
  85.                     tot := 0;
  86.                     { If the first character is a dollar sign, interpret this item as a hex number. }
  87.                     if fromPtr^ = dollar then
  88.                         begin
  89.                             base := 16;
  90.                             while not (fromPtr^ in [0,zero..nine,lowera..lowerf,upperA..upperF,comma]) do
  91.                                 fromPtr := pointer(ord4(fromPtr)+1);
  92.                         end;
  93.                     { Convert the string to a number. }
  94.                     while fromPtr^ in [zero..nine,lowera..lowerf,upperA..upperF] do
  95.                         begin
  96.                             theChar := fromPtr^;
  97.                             if theChar in [zero..nine] then tot := base*tot + theChar - zero
  98.                             else if theChar in [lowera..lowerf] then tot := base*tot + theChar - lowera + 10
  99.                             else tot := base*tot + theChar - upperA + 10;
  100.                             fromPtr := pointer(ord4(fromPtr)+1);
  101.                         end;
  102.                     { Get it as a byte. }
  103.                     theChar := SignedByte(tot);
  104.                     { Skip to the next comma. }
  105.                     while not (fromPtr^ in [0,comma]) do fromPtr := pointer(ord4(fromPtr)+1);
  106.                     { Skip past the comma. }
  107.                     if fromPtr^ <> 0 then fromPtr := pointer(ord4(fromPtr)+1);
  108.  
  109.                     { If we're copying, copy. }
  110.                     if doTheCopy then toPtr^ := theChar;
  111.                     toPtr := pointer(ord4(toPtr)+1);
  112.                 end;
  113.  
  114.             { Compute the final size of the output. }
  115.             copyAndCount := ord4(toPtr) - ord4(copyTo);
  116.         end;
  117.  
  118.     begin
  119.         { Check the parameter count. }
  120.         i := paramPtr^.paramCount;
  121.         if (i = 0) or (i > 2) then Fail('Invalid parameter count');
  122.  
  123.         { Check for an empty string being sent. }
  124.         if not ParmPresent(1) then exit(CTBSendBytes);
  125.         h := paramPtr^.params[1];
  126.  
  127.         { Make sure the Comm Toolbox is here. }
  128.         CTBReady;
  129.         { And so is a connection tool. }
  130.         EnsurePresent(connectionTool);
  131.         { And it's open. }
  132.         EnsureOpen;
  133.  
  134.         { Decide if we should set the EOM flag. }
  135.         if ParmPresent(2) then flags := cmFlagsEOM
  136.         else flags := 0;
  137.  
  138.         { Figure out the size of the new buffer and allocate it. }
  139.         ioBuffer := NewPtr(copyAndCount(h^,nil,false));
  140.         if ioBuffer = nil then Fail('Could not allocate buffer');
  141.  
  142.         { Copy the data into the buffer, and set the output request size. }
  143.         ioSize := copyAndCount(h^,ioBuffer,true);
  144.  
  145.         { Send the data to the port asynchronously. }
  146.         err := CMWrite(Globals^^.connHand,ioBuffer,ioSize,cmData,false,nil,-1,flags);
  147.         DisposPtr(ioBuffer);
  148.         if err <> noErr then Fail('Write failed');
  149.     end;
  150.  
  151. end.
  152.